bys b_id: gen n_b=_n 
bys b_id dataset: gen n_bc=_n 
egen gr_jc=group(dataset HS6_num)
egen gr_dj=group(top_d_b HS6)

bys b_id: egen num_c_b=nvals(dataset)
bys b_id: egen num_j_b=nvals(HS6_num)
bys b_id: egen num_jc_b=nvals(gr_jc)
bys b_id: egen num_s_b=nvals(s_id)
bys b_id: egen num_l_b=nvals(isid_g)
bys b_id dataset: egen num_j_bc=nvals(HS6_num)
bys b_id dataset: egen num_s_bc=nvals(s_id)
bys b_id dataset: egen num_l_bc=nvals(isid_g)

local O num_c_b num_j_b num_jc_b num_s_b num_l_b 
foreach o of local O{
eststo clear
estpost tabstat `o'  if n_b==1   ///
, listwise statistics(N mean sd p10 p25 median p75 p90)  columns(statistics)
esttab, cells("count(fmt(%9.0gc)) Mean(fmt(a2)) sd(fmt(a2)) p10(fmt(a2)) p25(fmt(a2)) p50(fmt(a2)) p75(fmt(a2)) p90(fmt(a2))") nomtitle nonumber noobs label varwidth(24) tex fragment
}

eststo O_TB6_A: estpost tabstat `O' if n_b == 1, statistics(N mean sd p10 p25 median p75 p90) columns(statistics)
esttab O_TB6_A using "${tables}\TableB6PanelA.tex", cells("count(fmt(%9.0gc)) Mean(fmt(a2)) sd(fmt(a2)) p10(fmt(a2)) p25(fmt(a2)) p50(fmt(a2)) p75(fmt(a2)) p90(fmt(a2))")  nomtitles label nonumbers noobs unstack replace posthead(" ") nodep collabels(none) prehead(" ") postfoot(" ")

 
capture drop xx
gen xx=(num_c_b>=2)
capture drop yy
gen yy=pq_exp if xx==1 
capture drop zz
egen zz=total(yy)
egen ww=total(pq_exp)
replace zz=zz/ww
sum zz
drop xx yy zz ww 
 
local O num_c_b num_j_b num_jc_b num_s_b num_l_b 
foreach o of local O{
eststo clear
estpost tabstat `o'  if n_b==1 & num_c_b>=2   ///
, listwise statistics(N mean sd p10 p25 median p75 p90)  columns(statistics)
esttab, cells("count(fmt(%9.0gc)) Mean(fmt(a2)) SD(fmt(a2)) p10(fmt(a2)) p25(fmt(a2)) p50(fmt(a2)) p75(fmt(a2)) p90(fmt(a2))") nomtitle nonumber noobs label varwidth(24) tex fragment
}

eststo O_TB6_B1: estpost tabstat `O' if n_b==1 & num_c_b>=2 , statistics(N mean sd p10 p25 median p75 p90) columns(statistics)
esttab O_TB6_B1 using "${tables}\TableB6PanelB1.tex", cells("count(fmt(%9.0gc)) Mean(fmt(a2)) SD(fmt(a2)) p10(fmt(a2)) p25(fmt(a2)) p50(fmt(a2)) p75(fmt(a2)) p90(fmt(a2))")  nomtitles label nonumbers noobs unstack replace posthead(" ") nodep collabels(none) prehead(" ") postfoot(" ")

local O num_j_bc num_s_bc num_l_bc 
foreach o of local O{
eststo clear
estpost tabstat `o'  if n_bc==1 & num_c_b>=2   ///
, listwise statistics(N mean sd p10 p25 median p75 p90)  columns(statistics)
esttab, cells("count(fmt(%9.0gc)) Mean(fmt(a2)) SD(fmt(a2)) p10(fmt(a2)) p25(fmt(a2)) p50(fmt(a2)) p75(fmt(a2)) p90(fmt(a2))") nomtitle nonumber noobs label varwidth(24) tex fragment
}

eststo O_TB6_B2: estpost tabstat `O' if n_bc==1 & num_c_b>=2, statistics(N mean sd p10 p25 median p75 p90) columns(statistics)
esttab O_TB6_B2 using "${tables}\TableB6PanelB2.tex", cells("count(fmt(%9.0gc)) Mean(fmt(a2)) SD(fmt(a2)) p10(fmt(a2)) p25(fmt(a2)) p50(fmt(a2)) p75(fmt(a2)) p90(fmt(a2))")  nomtitles label nonumbers noobs unstack replace posthead(" ") nodep collabels(none) prehead(" ") postfoot(" ")